package cn.bnuz.edu.liyang.wait_eat_what;

import android.app.Instrumentation;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import org.w3c.dom.Text;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Detail extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail);

        //返回按钮
        Button backbutton = (Button)findViewById(R.id.backbutton);
        backbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread() {
                    public void run() {
                        try {
                            Instrumentation instrument = new Instrumentation();
                            instrument.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
                        } catch (Exception e) {
                        }            }        }.start();    }});

        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("detail");
        Things detail = (Things)bundle.getSerializable("detail");

        ImageView detail_img = (ImageView) findViewById(R.id.detail_img);
        TextView detail_name = (TextView)findViewById(R.id.detail_name);
        TextView detail_comment = (TextView)findViewById(R.id.detail_comment);
        TextView detail_from = (TextView)findViewById(R.id.detail_from);
        TextView detail_price = (TextView)findViewById(R.id.detail_price);
        TextView detail_stars = (TextView)findViewById(R.id.detail_stars);


        URL imgUrl = null;
        try {
            imgUrl = new URL(detail.getImgPath());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) imgUrl
                    .openConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
        conn.setDoInput(true);
        try {
            conn.connect();
        } catch (IOException e) {
            e.printStackTrace();
        }
        InputStream is = null;
        try {
            is = conn.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Bitmap bitmap = BitmapFactory.decodeStream(is);
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        detail_img.setImageBitmap(bitmap);
        detail_name.setText(detail.getName());
        detail_from.setText(detail.getStoreName());

    }
}
